package com.xm.xmapp;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.xm.xmapp.adapter.ImageBucketAdapter;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.Type.A_ImageBucketChoose;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.response.bean.CustomImageBucket;
import com.xm.xmapp.tools.ImageFetcher;

public class ImageBucketChooseActivity extends BaseActivity {
	private ImageFetcher mHelper;
	private List<CustomImageBucket> mDataList = new ArrayList<CustomImageBucket>();
	private ListView mListView;
	private ImageBucketAdapter mAdapter;
	private int availableSize;
	/**
	 * 0是车辆问题反馈，1是充电桩问题反馈
	 */
	private int flag;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image_bucket_choose);
	}

	/**
	 * 0 ImageChooseActivity调用
	 */
	@Override
	public Object dataChang(int flag, Object data) {

		switch (flag) {
		case A_ImageBucketChoose.DEFAULT:
			finish();
			break;
		default:
			break;
		}
		return super.dataChang(flag, data);
	}

	public void initView() {
		mHelper = ImageFetcher.getInstance(getApplicationContext());
		mDataList = mHelper.getImagesBucketList(true);
		availableSize = getIntent().getIntExtra(CodeConstant.EXTRA_CAN_ADD_IMAGE_SIZE, CodeConstant.MAX_IMAGE_SIZE);
		mListView = (ListView) findViewById(R.id.listview);
		mAdapter = new ImageBucketAdapter(this, mDataList);
		mListView.setAdapter(mAdapter);
		flag = getIntent().getIntExtra("flag", 0);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				selectOne(position);
				Intent intent = new Intent(ImageBucketChooseActivity.this, ImageChooseActivity.class);
				intent.putExtra(CodeConstant.EXTRA_IMAGE_LIST, (Serializable) mDataList.get(position).imageList);
				intent.putExtra(CodeConstant.EXTRA_BUCKET_NAME, mDataList.get(position).bucketName);
				intent.putExtra(CodeConstant.EXTRA_CAN_ADD_IMAGE_SIZE, availableSize);
				intent.putExtra("flag", flag);
				startActivity(intent);

			}
		});

	}

	private void selectOne(int position) {
		int size = mDataList.size();
		for (int i = 0; i != size; i++) {
			if (i == position)
				mDataList.get(i).selected = true;
			else {
				mDataList.get(i).selected = false;
			}
		}
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void initData() {
		setMyTitle("选择相册");
	}

	@Override
	public void initEvent() {

	}

}
